package com.ogawa.projectcommon.widget

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import com.ogawa.projectcommon.R

import com.ogawa.projectcommon.activity.BaseMqttActivity
import kotlinx.android.synthetic.main.layout_movement_width_lev.view.*


//机芯宽度
class MovementWidthLevelView : FrameLayout, View.OnClickListener {
    private var lev = 0
    private var func: String = ""
    private lateinit var activity: BaseMqttActivity

    constructor(context: Context) : super(context)

    @SuppressLint("CustomViewStyleable")
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        LayoutInflater.from(context).inflate(R.layout.layout_movement_width_lev, this)
        tv_1.setOnClickListener(this)
        tv_2.setOnClickListener(this)
        tv_3.setOnClickListener(this)

        val a = context.obtainStyledAttributes(attrs, R.styleable.LevelViewAttr)
//        tv_lev_text.text = a.getString(R.styleable.LevelViewAttr_title)
        a.recycle()
        if (context is BaseMqttActivity) {
            activity = context
        }
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    fun setFunc(func: String) {
        this.func = func
    }

    @SuppressLint("SetTextI18n")
    fun setLev(lev: Int) {
        this.lev = lev
        tv_1.isSelected = lev == 1
        tv_2.isSelected = lev == 2
        tv_3.isSelected = lev == 3

        tv_lev.text = when(lev){
            0->{context.resources.getString(R.string.unadjustable)}
            1->{context.resources.getString(R.string.lev_width_wide)}
            2->{context.resources.getString(R.string.lev_width_wide)}
            3->{context.resources.getString(R.string.lev_width_wide)}
            else->{
                ""
            }
        }

    }

    fun setTitle(title: String) {
        tv_lev_text.text = title
    }

    override fun onClick(v: View?) {
        if (!activity.isSnExist()){
            return
        }
        if (func.isEmpty() || lev == 0 || lev == -1) {
            return
        }
        when (v?.id) {
            R.id.tv_1 -> activity.publish(func, "10", "1")
            R.id.tv_2 -> activity.publish(func, "10", "2")
            R.id.tv_3 -> activity.publish(func, "10", "3")
        }
    }
}